%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SVM Decoding
%
% GE_Step1_classifierapply.m
%
% Modified from Dimitrios Pantazis's code 
%  by SA 2018-03-30, 2018-04-11
%
% Adapted to the "32sclices" analysis 
%  by SA 2019-08-13
%    - removed all plotting to be able to run in a terminal..
%
% Modified to allow for multiple condition breakdowns
%  by DS 2021-03
%	 - cleaned up plotting variables
%
% Modified to run subjects in parallel
%  by DS 2021-04-08
%

%initialize
function GE_Step1_classifierapply_calconly_iter(iter, condition_tag)

svm_dir = '/autofs/space/clive_001/users/adriana/GE_SVM';
scripts_dir = sprintf('%s/scripts', svm_dir);
functions_dir = sprintf('%s/scripts/Functions', svm_dir);

%cd(svm_dir);

addpath(genpath(scripts_dir));
addpath(genpath(functions_dir));

SubjectNames = {'GE_05', 'GE_06', 'GE_07', 'GE_08', 'GE_09', 'GE_10', 'GE_11', 'GE_12', 'GE_13', 'GE_14', 'GE_15', 'GE_16'}; % 
%SubjectNames = {'GE_07'};
n_subjects = length(SubjectNames);

%analysis_tag = '_neighbors_vs_seeds_32slices'
analysis_tag = '';

% Specify the Regions of Interest ("ROIs", also called "labels":
%roiset_array = {'pMTG'};
%roiset_array = {'LIFG', 'SMG'};
%roiset_array = {'SMG', 'pMTG'};
roiset_array = {'newSMG', 'newpMTG', 'TempPole', 'FrontalPole', 'CentGyri'};

n_roisets = length(roiset_array);

% Specify the condition pairs:
switch condition_tag
    case 'half'
        %Divide data in half randomly (while maintaining proportion of words
        %to nonwords) for training
        train_conditionA_array = {'101', '101', '101', '101', '101', '201', '201', '201', '201', '301', '301', '301', '401', '401', '501',...
                                    '102', '102', '102', '102', '102', '202', '202', '202', '202', '302', '302', '302', '402', '402', '502'};
        train_conditionB_array = {'201', '301', '401', '501', '601', '301', '401', '501', '601', '401', '501', '601', '501', '601', '601',...
                                    '202', '302', '402', '502', '602', '302', '402', '502', '602', '402', '502', '602', '502', '602', '602'};
        test_conditionA_array = {'1','1','1','1','1','2','2','2','2','3','3','3','4','4','5',...
									'1','1','1','1','1','2','2','2','2','3','3','3','4','4','5'};
		test_conditionB_array = {'2','3','4','5','6','3','4','5','6','4','5','6','5','6','6',...
									'2','3','4','5','6','3','4','5','6','4','5','6','5','6','6'};
    case 'third'
        %Divide data in thirds randomly (while maintaining proportion of
        %tokens changed at each position) for training
        train_conditionA_array = {'1010', '1010', '1010', '1010', '1010', '2010', '2010', '2010', '2010', '3010', '3010', '3010', '4010', '4010', '5010',...
									'1020', '1020', '1020', '1020', '1020', '2020', '2020', '2020', '2020', '3020', '3020', '3020', '4020', '4020', '5020',...
									'1030', '1030', '1030', '1030', '1030', '2030', '2030', '2030', '2030', '3030', '3030', '3030', '4030', '4030', '5030'};
		train_conditionB_array = {'2010', '3010', '4010', '5010', '6010', '3010', '4010', '5010', '6010', '4010', '5010', '6010', '5010', '6010', '6010',...
									'2020', '3020', '4020', '5020', '6020', '3020', '4020', '5020', '6020', '4020', '5020', '6020', '5020', '6020', '6020',...
									'2030', '3030', '4030', '5030', '6030', '3030', '4030', '5030', '6030', '4030', '5030', '6030', '5030', '6030', '6030'};
		test_conditionA_array = {'1','1','1','1','1','2','2','2','2','3','3','3','4','4','5',...
									'1','1','1','1','1','2','2','2','2','3','3','3','4','4','5',...
									'1','1','1','1','1','2','2','2','2','3','3','3','4','4','5'};
		test_conditionB_array = {'2','3','4','5','6','3','4','5','6','4','5','6','5','6','6',...
									'2','3','4','5','6','3','4','5','6','4','5','6','5','6','6',...
									'2','3','4','5','6','3','4','5','6','4','5','6','5','6','6'};
end

n_condpairs = min(length(train_conditionA_array), length(train_conditionB_array));

plot_only = 0;   % compute and visualize

%-----------------------------------------------------------------
parpool(12);
    
parfor (i_subject = 1:length(SubjectNames), 12) %for all subjects
    %i_subject = 1;
    subject = char(SubjectNames(i_subject));
    %subject_dir=char(SubjectDirectories(i_subject));
    subject_dir = char(strcat(subject, analysis_tag));

    % Specify parameters
	param = struct('data_type', 'SOURCE', 'f_lowpass', 30, 'num_permutations', 100, 'trial_bin_size', 8, 'brainstorm_db', ['/autofs/space/clive_001/users/adriana/GE_SVM/' subject_dir]);
	%param.data_type = 'SOURCE';
	%param.f_lowpass = 30;
	%param.num_permutations = 100;
	%param.trial_bin_size = 10;
	%param.trial_bin_size = 8; %used for 3 vs. 4
    %param.brainstorm_db = sprintf('/autofs/space/clive_001/users/adriana/GE_SVM/%s',subject_dir); %directory of 'data' folder
    
    for i_condpair = 1:n_condpairs
    
        for i_roiset = 1:n_roisets

            roiset = char(roiset_array(i_roiset));

            train_conditionA= char(train_conditionA_array(i_condpair));
            train_conditionB= char(train_conditionB_array(i_condpair));
            test_conditionA= char(test_conditionA_array(i_condpair));
            test_conditionB= char(test_conditionB_array(i_condpair));

            %Run SVM analysis

            epochfile = sprintf('%s/%s/epochs/%s_%s_tokens_all.mat', svm_dir, subject_dir, subject, roiset);
            recodedfile = sprintf('%s/%s/epochs/recoded_events/%s_%s_%s%d_all.mat', svm_dir, subject_dir, subject, roiset, condition_tag, iter);
            
            %[accuracy,Time] = svm_contrast_conditions_perm(SubjectNames{i},condA,condB,param);
            %[accuracy,Time] = GE_svm_contrast_conditions_perm(epochfile,condA,condB,param);
            [accuracy,Time] = GE_svm_contrast_conditions_perm_diftest_iter(epochfile,recodedfile,train_conditionA,train_conditionB,test_conditionA,test_conditionB,param);
            
            

            resultsdir = sprintf('%s/%s/results/%s%d', svm_dir, subject_dir, condition_tag, iter);
            mkdir(resultsdir);
            resultsfile = sprintf('%s/%s_%s_train%sand%s_test%svs%s_Accuracy.mat', resultsdir, subject, roiset,train_conditionA,train_conditionB,test_conditionA,test_conditionB)

            
            %save(resultsfile,'Accuracy','Time','param');
            parsave(resultsfile, accuracy, Time, param); % save each subject's results separately
            

        end % i_roiset
    
    end % i_condition

end % i_subject

end %function

function parsave(fname, x,y,z)
	save(fname, 'x', 'y', 'z')
end



